package com.hitalkie.teacher.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hitalkie.teacher.R;
import com.hitalkie.teacher.model.Lesson;
import com.hitalkie.teacher.model.User;
import com.hitalkie.teacher.util.Utils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Date 24/09/2016
 *
 * @author charliema
 */

public class MainRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    static final int ITEM_TYPE_TITLE = 0;
    static final int ITEM_TYPE_ITEM_ALL = 1;
    static final int ITEM_TYPE_ITEM_MINE = 2;

    private List<Lesson> mDataMine = new ArrayList<>();
    private List<Lesson> mDataAll = new ArrayList<>();

    private OnItemClickListener mOnItemClick;

    public void setData(List<Lesson> mine, List<Lesson> all) {
        if (mine != null) {
            this.mDataMine = mine;
        }
        if (all != null) {
            this.mDataAll = all;
        }
        notifyDataSetChanged();
    }

    public void addData(List<Lesson> all) {
        if (all != null) {
            this.mDataAll.addAll(all);
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClick) {
        mOnItemClick = onItemClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ITEM_TYPE_TITLE:
                return new TitleViewHolder(parent);
            case ITEM_TYPE_ITEM_MINE:
            case ITEM_TYPE_ITEM_ALL:
                return new ViewHolder(parent);
            default:
                return new TitleViewHolder(parent);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ITEM_TYPE_TITLE:
                ((TitleViewHolder) holder).bindData(position);
                break;
            case ITEM_TYPE_ITEM_MINE:
            case ITEM_TYPE_ITEM_ALL:
                ((ViewHolder) holder).bindData((Lesson) getItem(holder.getAdapterPosition()));
                break;
            default:
                ((TitleViewHolder) holder).bindData(position);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE_TITLE;
        } else if (position > 0 && position < 1 + mDataMine.size()) {
            return ITEM_TYPE_ITEM_MINE;
        } else if (position == 1 + mDataMine.size()) {
            return ITEM_TYPE_TITLE;
        } else if (position > 1 + mDataMine.size() && position < 1 + mDataMine.size() + 1 + mDataAll.size()) {
            return ITEM_TYPE_ITEM_ALL;
        } else {
            return ITEM_TYPE_TITLE;
        }
    }

    @Override
    public int getItemCount() {
        return 1 + mDataMine.size() + 1 + mDataAll.size();
    }

    public Object getItem(int position) {
        if (position == 0) {
            return "Appointment";
        } else if (position > 0 && position < 1 + mDataMine.size()) {
            return mDataMine.get(position - 1);
        } else if (position == 1 + mDataMine.size()) {
            return "Classes";
        } else if (position > 1 + mDataMine.size() && position < 1 + mDataMine.size() + 1 + mDataAll.size()) {
            return mDataAll.get(position - (1 + mDataMine.size() + 1));
        } else {
            return "";
        }
    }


    /**
     * view holder
     */
    private class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivImage;
        ImageView ivLayerExpired;
        TextView tvName;
        ImageView ivTeacherAvatar;
        TextView tvTeacher;
        TextView tvBio;
        TextView tvTime;
        ImageView ivLive;

        ViewHolder(ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_lesson, parent, false));

            ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
            ivLayerExpired = (ImageView) itemView.findViewById(R.id.ivLayerExpired);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            ivTeacherAvatar = (ImageView) itemView.findViewById(R.id.ivTeacherAvatar);
            tvTeacher = (TextView) itemView.findViewById(R.id.tvTeacher);
            tvBio = (TextView) itemView.findViewById(R.id.tvBio);
            tvTime = (TextView) itemView.findViewById(R.id.tvTime);
            ivLive = (ImageView) itemView.findViewById(R.id.ivLive);
        }

        void bindData(final Lesson data) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClick != null) {
                        mOnItemClick.onItemClick(data);
                    }
                }
            });
//            if (data.topic != null && !TextUtils.isEmpty(data.topic.cover)) {
//                Picasso.with(ivImage.getContext()).load(data.topic.cover)
//                        .placeholder(R.drawable.ic_default_image)
//                        .error(R.drawable.ic_default_image)
//                        .fit().centerCrop().into(ivImage);
//            }
            tvName.setText(data.topic.getTitle());
            if (data.teachers != null && data.teachers.size() > 0 && data.teachers.get(0) != null) {
                // 有老师
                User teacher = data.teachers.get(0);
                if (!TextUtils.isEmpty(teacher.avatar)) {
                    // 有头像
                    Picasso.with(ivTeacherAvatar.getContext()).load(data.teachers.get(0).avatar)
                            .placeholder(R.drawable.ic_default_avatar)
                            .error(R.drawable.ic_default_avatar)
                            .fit().centerCrop().into(ivTeacherAvatar);
                } else {
                    // 没有头像
                    if (teacher.gender == 1) {
                        ivTeacherAvatar.setImageResource(R.drawable.ic_default_avatar_male);
                    } else if (teacher.gender == 2) {
                        ivTeacherAvatar.setImageResource(R.drawable.ic_default_avatar_female);
                    } else {
                        ivTeacherAvatar.setImageResource(R.drawable.ic_default_avatar);
                    }
                }

            } else {
                // 没老师
                ivTeacherAvatar.setImageResource(R.drawable.ic_default_avatar);
            }
            if (data.teachers != null && data.teachers.size() > 0 && data.teachers.get(0) != null && !TextUtils.isEmpty(data.teachers.get(0).name)) {
                tvTeacher.setText(data.teachers.get(0).name);
            } else {
                tvTeacher.setText(tvTeacher.getResources().getString(R.string.no_teacher));
            }
            if (data.teachers != null && data.teachers.size() > 0 && data.teachers.get(0) != null && !TextUtils.isEmpty(data.teachers.get(0).home_country)) {
                tvBio.setText("from:"+data.teachers.get(0).home_country); // TODO language
            } else {
                tvBio.setText("");
            }

            // 时间和状态
// status和status_dispaly说明：
// status用来判断课程状态，课程状态为1情况下该课程未开始， 通过start_time字段显示课程开始时间，
// status不为1时直接显示status_display字段，
// 这时后台可以根据header中的选择语言返回课程状态，
//  即将开始（status=10） 进行中(status=2) 已结束(status=3)
            if (data.status == 1) {
                tvTime.setText(Utils.getUpdateTime(Long.parseLong(data.start_time)));
            } else {
                tvTime.setText(data.status_display);
            }
            if (data.status == 2) {
                ivLive.setImageResource(R.drawable.ic_live_on);
            } else {
                ivLive.setImageResource(R.drawable.ic_live_off);
            }
            // 丝袜效果
            if(data.status == 3){
                ivLayerExpired.setVisibility(View.VISIBLE);
            }else{
                ivLayerExpired.setVisibility(View.INVISIBLE);
            }

        }
    }

    /**
     *
     */
    private class TitleViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;

        TitleViewHolder(ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_title, parent, false));
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
        }

        void bindData(int position) {
//            getAdapterPosition()
            if (position == 0) {
                if (mDataMine.size() == 0) {
                    tvTitle.setVisibility(View.GONE);
                } else {
                    tvTitle.setVisibility(View.VISIBLE);
                }
            } else {
                tvTitle.setVisibility(View.VISIBLE);
            }
            tvTitle.setText((String) getItem(position));
        }


    }

    /**
     * click event
     */
    public interface OnItemClickListener {
        void onItemClick(Lesson data);

    }


}
